package ${package}.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import ${package}.domain.UserDomain;
import ${package}.exception.BusinessException;
import ${package}.repository.UserRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(value = "/user", description = "user接口列表")
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserRepository userRepository;
	
    @ApiOperation(value = "获取用户信息列表", notes = "根据ID返回用户信息", httpMethod = "GET")
    @RequestMapping(value = "/v1.0/{name}", method = RequestMethod.GET)
    public String getByName(@PathVariable("name") String name) {
    	UserDomain u = userRepository.getByName(name);
    	if(null==u){
    		throw new BusinessException("查询不到数据",500);
    	}
        return null==u? "no user found": u.toString();
    }
    
    @ApiOperation(value = "获取用户信息列表", notes = "根据ID返回用户信息", httpMethod = "GET")
    @RequestMapping(value = "/v1.0/", method = RequestMethod.GET)
    public List<UserDomain> getList() {
    	Iterable<UserDomain> it = userRepository.findAll();
    	List<UserDomain> list = Lists.newArrayList(it);
    	if(null==list){
    		throw new BusinessException("查询不到数据",500);
    	}
        return list;
    }
    
    
    @ApiOperation(value = "保存用户信息", notes = "保存用户信息！！", httpMethod = "POST")
    @RequestMapping(value = "/v1.0/", method = RequestMethod.POST)
    public String save(@RequestBody UserDomain user) {
    	userRepository.save(user);
    	System.out.println(userRepository.findOne(user.getId()));
        return "success";
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息！！", httpMethod = "PUT")
    @RequestMapping(value = "/v1.0/{id}", method = RequestMethod.PUT)
    public String modify(@PathVariable("id") Long id, @RequestBody UserDomain user) {
    	UserDomain u = userRepository.findOne(id);
    	if(null==u){
    		return "user not found";
    	}
    	u.setName(user.getName());
    	userRepository.save(u);
        return "success";
    }

    @ApiOperation(value = "删除用户信息", notes = "删除用户信息！！", httpMethod = "DELETE")
    @RequestMapping(value = "/v1.0/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Long id) {
    	userRepository.delete(id);
        return "success";
    }

}
